﻿using System;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;
using UnityEngine.UI;

public class TipsController : MonoBehaviour
{
    public static TipsController _instance;
    public Text content;
    private Tween tempTween;
    private float CountDownTime = 10;

    void Awake()
    {
        _instance = this;
        content = transform.GetComponentInChildren<Text>();
        tempTween = content.DOFade(1, 2);
        tempTween.SetLoops(2, LoopType.Yoyo);
        tempTween.SetAutoKill(false);
        tempTween.Pause();
    }

    public void ShowNewTipContent(string message)
    {
        content.text = message;
        tempTween.Restart();
    }

    /// <summary>
    /// 倒计时  5秒
    /// </summary>
    public void CountDown(Action endAction = null)
    {
        //todo 倒计时动画效果
        Tween punchTween = content.rectTransform.DOPunchScale(Vector3.one * 2, 0.3f, 5);
        punchTween.SetAutoKill(false);
        punchTween.Pause();
        Tween FadeOn = content.DOFade(1, 0.2f);
        FadeOn.SetAutoKill(false);
        FadeOn.OnPlay(delegate
        {
            if (!FadeOn.isBackwards)
            {
                int time = -1;
                //正序播放渐变动画时，播放倒计时文字动画
                Tween CountDownTween = DOTween.To(() => CountDownTime, x => CountDownTime = x, 0, CountDownTime);
                CountDownTween.SetEase(Ease.Linear);
                CountDownTween.OnUpdate(delegate
                {
                    int temp = Mathf.FloorToInt(CountDownTime);
                    content.text = temp + "";
                    if (time != temp)
                    {
                        punchTween.Restart();
                        time = temp;
                    }
                });
                CountDownTween.OnComplete(delegate
                {
                    FadeOn.PlayBackwards();
                });
            }
            FadeOn.OnStepComplete(delegate
            {
                if (FadeOn.isBackwards)
                {
                    //完全消失后重置倒计时时长
                    CountDownTime = 10;
                    if(endAction != null)
                        endAction();
                }
            });
        });
    }
}
